<?php
/**
 * FileName: Helper.php
 * Author: Fei.Song
 * Date: 2021/3/4 14:36
 * Created by PhpStorm
 * Desc:自定义辅助函数,函数可以直接调用(模板中也可以)
 */

/**
 * 数据标签用法
 * 可以任意位置调用,主要用于模板上数据获取
 */

use App\Service\UpLoad;

/**
 * 数据标签用法
 * 可以任意位置调用,主要用于模板上数据获取
 */
if (!function_exists('getData')) {
    /**
     * @param $className 数据查询类名
     * @param array $parameter 查询方法参数数组
     * @return mixed
     * @throws Exception
     */
    function getData($className, array $parameter = []) {
        $className = "\App\Lib\DataTag\\" . $className;
        if (class_exists($className)) {
            $obj = new $className();
            return $obj->getDataService($parameter);
        } else {
            throw new Exception("Error: '{$className}' not found");
        }
    }
}

/**
 * 自定义权限检测
 * @params $permissionName 权限名称
 */
if (!function_exists("_can")) {
    function _can($permissionName) {
        $mark = preg_replace("/.attach(.[\d]+){0,1}$/", "", $permissionName);
        return auth()->guard(session('guard'))->user()->can($mark);
    }
}

/**
 * 获取当前管理的站点ID
 */
if (!function_exists("getCurrentSiteId")) {
    function getCurrentSiteId() {
        $guard = session('guard') ? session('guard') : "web";
        $user = auth()->guard($guard)->user();
        if(empty($user)){
            return session('siteId');
        }else{
            return $user->site_id;
        }
//        return empty($user) ? -1 : $user->site_id;
//        return session("siteId");
        //Todo 修改站点ID获取方法
    }
}

/**
 * 获取当前登录的用户
 */
if (!function_exists("getCurrentUser")) {
    function getCurrentUser() {
        $guard = session('guard') ? session('guard') : "web";
        $user = auth()->guard($guard)->user();
        return empty($user) ? [] : $user->toArray();
//        return auth()->user()->toArray();
    }
}

/**
 * 数据字典转换
 */
if (!function_exists("L")) {
    /**
     * @param $field
     * @param null $default
     * @return string
     */
    function L($field, $default = null) {
        return config("dict_zh_CN.{$field}") ? config("dict_zh_CN.{$field}") : $default;
    }
}

/**
 * 上传图片
 * @param $file 上传文件
 * @param $path 路径
 * @author 郭碧云
 */
if (!function_exists("uploadImg")) {
    function uploadImg($file, $path, $position = "local",$type = 0) {
        /*$result = (new UpLoad())->up_img($file, config('storage.rootimg') . $path, $position);
        if ($result) {
            return ["error" => 0, "path" => $result, "msg" => "上传成功"];
        }
        return ["error" => 100000, "msg" => "上传失败", "path" => $result];*/
        $result = (new UpLoad())->up_img($file, config('storage.rootimg') . $path, $position);
        if ($result) {
            return $type==0?["error" => 0, "path" => $result, "msg" => "上传成功"]:[
                "uploaded" => 1,
                "fileName" => '',
                "url" => "/".$result,
                "error" => [
                    "message" => "上传成功"
                ]
            ];
        }
        return $type==0?["error" => 100000, "msg" => "上传失败", "path" => $result]:[
            "uploaded" => 1,
            "fileName" => '',
            "url" => "/".$result,
            "error" => [
                "message" => "上传失败"
            ]];
    }
}

/**
 * 前端js路由
 * 获取路由路径
 * @param $name 路由名称
 */
if (!function_exists("route_uri")) {
    function route_uri($name) {
        return app('router')->getRoutes()->getByName($name)->uri();
    }
}



